package com.base.controller;

import com.base.comm.CommUrls;
import com.base.po.voip.*;
import com.base.service.*;
import com.base.utils.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 自营商城订单
 *
 * @author Administrator
 */
@Controller
@RequestMapping("/admin")
public class index_TourOrder {
    @Autowired
    private TourOrderService tourOrderService;
    @Autowired
    private UsersService usersService;
    @Autowired
    private ExpressDeliveryService expressDeliveryService;

    // 旅游订单首页
    @RequestMapping("/tourOrder")
    public ModelAndView index(HttpServletRequest request) throws Exception {
        // 返回ModelAndView
        ModelAndView modelAndView = new ModelAndView();
        String adminUserId = (String) request.getSession().getAttribute("adminUserId");
        Integer adminUsergrade = (Integer) request.getSession().getAttribute("adminUsergrade");
        Page page = new Page();
        Users users = usersService.selectByPrimaryKey(adminUserId);

        //已有订单
        List<TourOrder> tourOrders = null;
        page.setCurrentPageNo(1);
        if (users.getRole() == 0 || users.getRole() == 1) {
            page.setRecordTotal(tourOrderService.countByAll(adminUserId, adminUserId, null, null, null, null, null, null, null, users.getRole()));
            tourOrders = tourOrderService.selectByAll(page, adminUserId, adminUserId, null, null, null, null, null, null, null,users.getRole());
        }

        if (CommUrls.THISSERVER.equals("http://xt.sbdznkj.com/SbdVoip/")) {
            request.setAttribute("exportType", 2);
        } else {
            request.setAttribute("exportType", 1);
        }

        List<ExpressDelivery> expressDeliveries = expressDeliveryService.selectAll();

        request.setAttribute("totlenum", page.getRecordTotal()); // 分页
        request.setAttribute("curPage", page.getCurrentPageNo());// 当前页面传入session中
        request.setAttribute("selfShopOrder", tourOrders);
        request.setAttribute("expressDeliveries", expressDeliveries);
        request.setAttribute("adminUsergrade", adminUsergrade);
        request.setAttribute("adminUserId", adminUserId);
        request.setAttribute("status", 0);
        request.setAttribute("deliverystatus", 2);
        request.setAttribute("defaultPageSize",10);
        // 设置页面进行跳转
        modelAndView.setViewName("jsp/index_TourOrder");
        return modelAndView;
    }


    @RequestMapping("/selectTourOrder")
    public ModelAndView selectTourOrder(HttpServletRequest request, Integer curPage, String goodstext,
                                            String ordernum, String mobile, Byte status, Byte deliverystatus, String supplier, String createtime) {
        // 设置session缓存
        if (curPage == null) {
            if (request.getSession().getAttribute("curPage") != null) {
                curPage = (Integer) request.getSession().getAttribute("curPage");
            } else {
                curPage = 1;
            }
        }
        Integer adminUsergrade = (Integer) request.getSession().getAttribute("adminUsergrade");
        String adminUserId = (String) request.getSession().getAttribute("adminUserId");

        ModelAndView modelAndView = new ModelAndView();
        Page page = new Page();
        Users users = usersService.selectByPrimaryKey(adminUserId);

        //已有商品
        List<TourOrder> tourOrders = null;
        Integer choose = 2;
        if (goodstext != null && !goodstext.equals("") || mobile != null && !mobile.equals("")
                || ordernum != null && !ordernum.equals("")
                || supplier != null && !supplier.equals("")
                || createtime != null && !createtime.equals("")
                || status != 0 || deliverystatus != 2) {
            choose = 1;
        }

        String fGoodsText = null;
        String fMobile = null;
        String fOrderNum = null;
        Byte fStatus = null;
        Byte fDeliveryStatus = null;
        String fSupplier = null;
        String fCreatetime = null;

        if (goodstext != null && !goodstext.equals("")) {
            fGoodsText = goodstext;
        }
        if (mobile != null && !mobile.equals("")) {
            fMobile = mobile;
        }
        if (ordernum != null && !ordernum.equals("")) {
            fOrderNum = ordernum;
        }
        if (status != 0) {
            fStatus = status;
        }
        if (deliverystatus != 2) {
            fDeliveryStatus = deliverystatus;
        }
        if (supplier != null && !supplier.equals("")) {
            fSupplier = supplier;
        }
        if (createtime != null && !createtime.equals("")) {
            fCreatetime = createtime;
        }

        page.setCurrentPageNo(curPage);
        if (users.getRole() == 0 || users.getRole() == 1) {
            page.setRecordTotal(tourOrderService.countByAll(adminUserId, adminUserId, fMobile, fOrderNum, fGoodsText,
                    fStatus, fDeliveryStatus, fSupplier, fCreatetime,users.getRole()));
            tourOrders = tourOrderService.selectByAll(page, adminUserId, adminUserId, fMobile, fOrderNum, fGoodsText,
                    fStatus, fDeliveryStatus, fSupplier, fCreatetime,users.getRole());
        }

        if (choose == 2) {
            if (users.getRole() == 0 || users.getRole() == 1) {
                page.setRecordTotal(tourOrderService.countByAll(adminUserId, adminUserId, null, null, null, null, null, null, null,users.getRole()));
                tourOrders = tourOrderService.selectByAll(page, adminUserId, adminUserId, null, null, null, null, null, null, null,users.getRole());
            }
        }

        if (CommUrls.THISSERVER.equals("http://xt.sbdznkj.com/SbdVoip/")) {
            request.setAttribute("exportType", 2);
        } else {
            request.setAttribute("exportType", 1);
        }

        List<ExpressDelivery> expressDeliveries = expressDeliveryService.selectAll();

        request.setAttribute("totlenum", page.getRecordTotal()); // 分页
        request.setAttribute("curPage", page.getCurrentPageNo());// 当前页面传入session中
        // 设置页面进行跳转
        modelAndView.setViewName("jsp/index_TourOrder");
        request.setAttribute("mobile", mobile);
        request.setAttribute("ordernum", ordernum);
        request.setAttribute("status", status);
        request.setAttribute("goodstext", goodstext);
        request.setAttribute("tourOrders", tourOrders);
        request.setAttribute("expressDeliveries", expressDeliveries);
        request.setAttribute("adminUsergrade", adminUsergrade);
        request.setAttribute("adminUserId", adminUserId);
        request.setAttribute("deliverystatus", deliverystatus);
        request.setAttribute("supplier", supplier);
        request.setAttribute("createtime", createtime);
        request.setAttribute("defaultPageSize",10);
        return modelAndView;
    }
}
